// posts.js
var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');

Page({
  onShareAppMessage: function () {
    return this.data.shareAppMessage;
  },
  data: {
    title: '话题详情',
    detail: {},
    hidden: false,
     shareAppMessage: {}
  },
  onLoad: function (options) {
    this.data.shareAppMessage = util.getShareAppMessage();
    this.data.id = options.id;
    this.fetchData(options.id);
  },
  onPullDownRefresh: function () {
    console.log('下拉刷新', new Date());
    this.fetchData(this.data.id);
  },
  imageLoad: function (e) {
    var imageSize = util.imageUtil(e)
    this.setData({
      imagewidth: imageSize.imageWidth,
      imageheight: imageSize.imageHeight
    })
  },
  fetchData: function (id) {
    console.log(id);
    var self = this;
    self.setData({
      hidden: false
    });
    var parameter = {};
    parameter.id = id;
    parameter.userId = util.getUserId();
    wx.request({
      url: Api.getTrialDetail(parameter),
      header: Api.headers,
      success: function (res) {
        console.log(JSON.stringify(res));
        res.data.data.endtime = util.build_time_ymd(res.data.data.endtime);
        res.data.data.introduction = util.removeHtmlTags(res.data.data.introduction);
        self.setData({
          detail: res.data.data,
          shareAppMessage: {
            title: "抢试用-瑞丽网 "+res.data.data.name,
            desc: res.data.data.introduction,
            path: '/pages/detail/detail?id=' + id
          }
        });
        setTimeout(function () {
          self.setData({
            hidden: true
          });
        }, 300);
      }
    });
  },
  //申请试用
  applyTrial: function (e) {
    if (util.checkLogin("", "phone")) {
      var isApply = e.target.dataset.apply;
      console.log('isApply - ' + isApply);
      if (isApply) {
        wx.showModal({
          title: '提示',
          content: "您已经提交过申请了",
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
        return;
      }
      var id = e.currentTarget.id,
        title = e.target.dataset.title,
        url = '../apply/apply?id=' + id + '&title=' + title;
      console.log('申请试用' + id + "-" + title);
      wx.navigateTo({
        url: url
      });
    }
  },
})
